//
//  LoadingView.m
//  MexicoLoan
//
//  Created by WXP on 2025/2/24.
//
//

#import "LoadingView.h"

@interface LoadingView ()

@end

@implementation LoadingView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.viewBack];
        self.viewBack.center = CGPointMake(self.width/2.0, self.height/2.0);
        [self.viewBack addSubview:self.imageLoading];
        self.userInteractionEnabled = true;
    }
    return self;
}
- (UIView *)viewBack{
    if (_viewBack == nil) {
        _viewBack = [UIView  new];
        _viewBack.widthHeight = XY(100, 100);
        [GlobalMethod setRoundView:_viewBack color:[UIColor clearColor] numRound:18 width:0];
        _viewBack.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.5];
    }
    return  _viewBack;
}
- (UIImageView *)imageLoading{
    if (!_imageLoading) {
        _imageLoading = [UIImageView new];
        _imageLoading.image = [UIImage imageNamed:@"icon-loading"];
        _imageLoading.widthHeight = XY(32, 32);
        _imageLoading.centerXCenterY = XY(self.viewBack.width/2, self.viewBack.height/2);
    }
    return _imageLoading;
}

- (void)resetFrame:(CGRect)frame viewShow:(UIView *)viewShow{
    self.frame = frame;
    self.viewBack.centerXCenterY = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2-Ratio(30));
    [viewShow addSubview:self];
    [self beginAnimate];
}

- (void)hideLoading{
    [self stopAnimate];
    [self removeFromSuperview];
}

#pragma mark - Animate
//begin animate
- (void)beginAnimate{
    [self stopAnimate];
    CABasicAnimation * animate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    animate.toValue = @(M_PI*2);
    [animate setRepeatCount:MAXFLOAT];
    [animate setDuration:2];
    [animate setRemovedOnCompletion:true];
    [self.imageLoading.layer addAnimation:animate forKey:@"animated"];
}

//stop animate
- (void)stopAnimate{
    [self.imageLoading.layer removeAllAnimations];
}

@end
